package com.wangtao.firstapp.activity;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.wangtao.firstapp.R;

/**
 * 点击头像弹出PopupWindow  点击修改昵称和个性签名后跳转到此页面
 * Created by lanouhn on 16/9/18.
 */
public class ModifyNameSignActivity extends AppCompatActivity {
    private TextView tv_close_modifyname;// 标题栏关闭按钮
    private TextView tv_save_modifyname;// 标题栏保存按钮
    private EditText et_name_modifyname;// 输入的昵称
    private EditText et_sign_modifyname;// 输入的个性签名
    private PopupWindow popupwindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.modifynamesign);
        initView();
        initEvent();
    }

    private void initView() {
//        tv_close_modifyname = (TextView) findViewById(R.id.tv_close_modifyname);// 标题栏关闭按钮
//        tv_save_modifyname = (TextView) findViewById(R.id.tv_save_modifyname);// 标题栏保存按钮
        //et_name_modifyname = (EditText) findViewById(R.id.et_name_modifyname);// 输入的昵称
        //et_sign_modifyname = (EditText) findViewById(R.id.et_sign_modifyname);// 输入的个性签名
        tv_close_modifyname = (TextView) findViewById(R.id.tv_close_modifyname);// 标题栏关闭按钮
        tv_save_modifyname = (TextView) findViewById(R.id.tv_save_modifyname);// 标题栏保存按钮
        et_name_modifyname = (EditText) findViewById(R.id.et_name_modifyname);// 输入的昵称
        et_sign_modifyname = (EditText) findViewById(R.id.et_sign_modifyname);// 输入的个性签名
        TextChange textChange = new TextChange();
        et_name_modifyname.addTextChangedListener(textChange);
        et_sign_modifyname.addTextChangedListener(textChange);
    }

    private void initEvent() {
        // 关闭按钮
        tv_close_modifyname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        // 保存按钮
        tv_save_modifyname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                show();
                CountDownTimer timer = new CountDownTimer(1500, 10) {
                    @Override
                    public void onTick(long millisUntilFinished) {

                    }

                    @Override
                    public void onFinish() {
                        finish();
                    }
                }.start();
            }
        });
    }

    // 监听EditText文本的变化
    public class TextChange implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//            String name = et_name_modifyname.getText().toString();
//            Log.e("name",name);
//            String sign = et_sign_modifyname.getText().toString();
//            Log.e("sign",sign);
//            Intent intent = new Intent();
//            intent.putExtra("name", name);
//            intent.putExtra("sign", sign);
//            setResult(102, intent);
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (et_name_modifyname.length() > 0 && et_sign_modifyname.length() > 0) {
                tv_save_modifyname.setClickable(true);
                tv_save_modifyname.setTextColor(getResources().getColor(R.color.mycolor1));
            }
            if (et_name_modifyname.length() == 0 || et_sign_modifyname.length() == 0) {
                tv_save_modifyname.setClickable(false);
                tv_save_modifyname.setTextColor(getResources().getColor(R.color.mycolor2));
            }

            String name = et_name_modifyname.getText().toString();
            String sign = et_sign_modifyname.getText().toString();
            Intent intent = new Intent();
            intent.putExtra("name", name);
            intent.putExtra("sign", sign);
            setResult(102, intent);

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }

    private void show() {
        View view = LayoutInflater.from(ModifyNameSignActivity.this).inflate(R.layout.image_popupwindow, null);
        popupwindow = new PopupWindow(view, 500, 300, true);
        popupwindow.setContentView(view);
        popupwindow.setFocusable(false);
        popupwindow.setBackgroundDrawable(new BitmapDrawable());
        popupwindow.setOutsideTouchable(true);
        View rootview = LayoutInflater.from(ModifyNameSignActivity.this).inflate(R.layout.modifynamesign, null);
        popupwindow.showAtLocation(rootview, Gravity.CENTER, 0, 0);
        CountDownTimer timer = new CountDownTimer(1500, 10) {

            @Override
            public void onTick(long millisUntilFinished) {
            }

            @Override
            public void onFinish() {
                popupwindow.dismiss();
            }
        }.start();
    }


}
